﻿using AutoMapper;
using B.S.BaseData.Domain.Entities.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.WriteAPI.Utils;
using B.S.DataBase.EFCoreServiers.Dtos.Depts;
using B.S.DataBase.EFCoreServiers.Dtos.Roles;
using B.S.DataBase.EFCoreServiers.IServices.Dtpes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Depts")]
    public class DepratmentController : ControllerBase
    {
        private readonly IDepartmentServices departmentServices;
        private readonly ILogger<DepratmentController> _logger;

        public DepratmentController(IDepartmentServices departmentServices, ILogger<DepratmentController> logger)
        {
            this.departmentServices = departmentServices;
            _logger = logger;
        }
        /// <summary>
        /// 部门新增
        /// </summary>
        /// <param name="departmentDto"></param>
        /// <returns></returns>
        [HttpPost]
        [LogOperation("部门管理", "新增", "添加新部门")]
        public async Task<ApiResult> AddDepartment(AddDepartmentDto departmentDto)
        {
            try
            {
                var res = await departmentServices.AddDepartmentAsync(departmentDto);
                if (res != null)
                {
                    _logger.LogInformation("开始添加部门: ", departmentDto.DepartmentName);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("添加部门失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        [HttpPut]
        [LogOperation("部门管理", "修改", "更新部门信息")]
        public async Task<ApiResult> UpdateDepartment([FromBody] UpdateDepartmentDto dept)
        {
            try
            {
                var res = await departmentServices.UpdateDepartmentAsync(dept);
                if (res != null)
                {
                    _logger.LogInformation("开始编辑部门: ", dept.DepartmentName);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("编辑部门失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        [HttpDelete]
        [LogOperation("部门管理", "删除", "删除部门")]
        public async Task<ApiResult> DeleteDepartment(int deptId)
        {
            try
            {
                var res = await departmentServices.DeleteDepartmentAsync(deptId);
                if (res != null)
                {
                    _logger.LogInformation("开始删除部门: ", deptId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("删除部门失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }

        /// <summary>
        /// 修改部门状态
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        [HttpPut]
        [LogOperation("部门管理", "状态修改", "修改部门启用状态")]
        public async Task<ApiResult> UpdateStateDeptAsync(int deptId)
        {
            try
            {
                var res = await departmentServices.UpdateStateDeptAsync(deptId);
                if (res != null)
                {
                    _logger.LogInformation("修改部门状态: ", deptId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult.Fail("修改部门状态失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }





    }
}
